/*
 * @ author madman 
 * @ use 购买号码
 */
// const NumbersModel = require('../../models/nexmo/numbers.model');
const Utils = require('../utils');
const PriceModel = require('../../models/nexmo/price.model');
class Price {
    // 新增
    async add(ctx) {
      const _body = ctx.request.body
      const _params = Utils._params(_body)
      const result = await PriceModel.create(_params);
      if (result) {
          return ctx.success({ msg: '添加成功' });
      } else {
          return ctx.error({ msg: '添加失败' });
      }
    }
    // 更新
    async update(ctx) {
      const _body = ctx.request.body
      const _params = Utils._params(_body)
      _params.updataAt = new Date()
      const result = await PriceModel.updateOne({ _id: _body._id }, { $set: _params });
      if (result) {
          return ctx.success({ msg: '更新成功' });
      } else {
          return ctx.error({ msg: '更新失败' });
      }
    }
    // 删除
    async delete(ctx) {
      const _id = ctx.query.id
      const result = await PriceModel.deleteOne({ _id: _id });
      if (result) {
          return ctx.success({ msg: '删除成功' });
      } else {
          return ctx.error({ msg: '删除失败' });
      }
    }
    /*
    * 批量删除方法
    */
    async batchdelete(ctx) {
        const arrids = ctx.request.body.arrids
        let result = await PriceModel.remove({ _id: { $in: arrids } })
        if (result) {
            return ctx.success({ msg: '删除成功' });
        } else {
            return ctx.error({ msg: '删除失败' });
        }
    }
    //获取所有价格配置
    async list(ctx) {
      const options = {
          name: 'nexmo/price',
          type: 'page',
          findField: {}
      }
      if (ctx.query.countryCode) {
        options.findField.countryCode = new RegExp(ctx.query.countryCode, 'i')
      }
      let result = null
      result = await Utils.getPageList(ctx, options)
      if (result) {
          return ctx.success({
              data: result,
              msg: '查询成功'
          });
      }
    }
}

export default new Price;